<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="用户名称" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入用户名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收益状态" prop="states">
        <el-select
          v-model="queryParams.states"
          placeholder="请选择收益状态"
          clearable
          style="width: 240px"
        >
          <el-option label="已结算" :value="1" />
          <el-option label="待结算" :value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="广告日期">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <p style="color: #606266; font-size: 14px; font-weight: 700">
          看广告总人数：{{ menberNum || 0 }}人
        </p>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="typeList">
      <el-table-column label="序号" type="index" align="center" />
      <el-table-column label="用户名称" align="center" prop="nickname" />
      <el-table-column label="手机号" align="center" prop="mobile" />
      <el-table-column label="收益状态" align="center" prop="states">
        <template slot-scope="scope">
          <p>{{ scope.row.states == 1 ? "已发放" : "待结算" }}</p>
        </template>
      </el-table-column>
      <el-table-column
        label="广告收益等级"
        align="center"
        prop="advertLevelName"
      />
      <el-table-column label="观看日期" align="center" prop="addTime">
        <template slot-scope="scope">
          <p>{{ parseTime(scope.row.addTime) }}</p>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 录入昨日总收益 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="昨日总收益金额" prop="totalRevenue">
          <el-input
            v-model="form.totalRevenue"
            type="number"
            :min="0"
            placeholder="请输入昨日总收益金额"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { taskSave, taskList, taskUpdate } from "@/api/advert/advert";
import { uploadFile } from "@/api/uploadFile";
import md5 from "js-md5";
import dayjs from "dayjs";

export default {
  name: "advert",
  data() {
    return {
      uploadImgUrl: process.env.VUE_APP_UPLOAD_API + "/file/uploadFile",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      typeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        nickname: null,
        mobile: null,
        states: null,
        beginTime: null,
        endTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        totalRevenue: [
          { required: true, message: "昨日总收益不能为空", trigger: "blur" },
        ],
      },
      progressPercent: 0,
      progressStatus: null,
      showProgress: false,
      saasToken: "",
      menberNum: 0,
    };
  },
  created() {
    let date = dayjs().format("YYYYMMDD");
    let saasToken = date + "MNAc1qMEyQXauIVckovoh5gklhQnziyZ" + 2;
    this.saasToken = md5(saasToken);
    this.getList();
  },
  methods: {
    /** 查询字典类型列表 */
    getList() {
      this.loading = true;
      if (this.dateRange.length > 0) {
        this.queryParams.beginTime = this.dateRange[0];
        this.queryParams.endTime = this.dateRange[1];
      }
      taskList(this.queryParams).then((res) => {
        this.typeList = res.data;
        this.menberNum = res.extend?.count || 0;
        this.total = res.extend.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        totalRevenue: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "录入昨日广告总收益";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.dictId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = row;
      this.open = true;
      this.title = "修改广告";
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          taskSave(this.form).then((response) => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.video-box {
  width: 100%;
  max-height: 400px;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

::v-deep .el-dialog__body {
  padding: 20px 40px;
  box-sizing: border-box;
}
</style>
